﻿/*
 * Created by SharpDevelop.
 * User: IEscarro
 * Date: 9/1/2009
 * Time: 11:46 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using Buaya.Model;

namespace Buaya.Gui
{
	/// <summary>
	/// Description of UserListForm.
	/// </summary>
	public partial class UserListForm : BaseForm
	{
		public UserListForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
		}
		
		[System.ComponentModel.EditorBrowsableAttribute()]
		protected override void OnActivated(EventArgs e)
		{
			base.OnActivated(e);
			Model = SessionFacade.FindAllUsers();
		}
		
		protected override void OnModelChanged(EventArgs e)
		{
			base.OnModelChanged(e);
			listBox1.Items.Clear();
			foreach (User u in Model as IList<User>) {
				listBox1.Items.Add(u.Name);
			}
		}
		
		void ButtonDeleteClick(object sender, EventArgs e)
		{
			if (listBox1.SelectedIndex >= 0) {
				User u = (Model as IList<User>)[listBox1.SelectedIndex];
				SessionFacade.DeleteUser(u);
				OnActivated(null);
			}
		}
		
		void ListBox1DoubleClick(object sender, EventArgs e)
		{
			if (listBox1.SelectedIndex >= 0) {
				User u = (Model as IList<User>)[listBox1.SelectedIndex];
				MainForm.Instance.AddDialog(new UserForm(u));
			}
		}
		
		void ButtonCloseClick(object sender, EventArgs e)
		{
			Close();
		}
		
		void ButtonAddClick(object sender, EventArgs e)
		{
			MainForm.Instance.AddDialog(new UserForm());
		}
		
		void ButtonEditClick(object sender, EventArgs e)
		{
			ListBox1DoubleClick(sender, e);
		}
	}
}
